﻿//3D Graphics with XNA studio 4.0 ISBN 978-1-849690-04-1 pg 258 - 260

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SkinnedModel;
namespace ProjectRoughWork
{
    class SkinnedModel
    {
        Model model;
        GraphicsDevice graphicsDevice;
        ContentManager content;

        SkinningData skinningData;
        public static bool firstRun = false;

        public float PlaySpeed = 1.5f;

        public Vector3 Position, Rotation, Scale;
        
        public Model Model { get { return model; } }

        public AnimationPlayer Player { get; private set; }

        public SkinnedModel(Model Model, Vector3 Position, Vector3 Rotation,
            Vector3 Scale, GraphicsDevice GraphicsDevice,
            ContentManager Content)
        {
            this.model = Model;
            this.graphicsDevice = GraphicsDevice;
            this.content = Content;
            this.Position = Position;
            this.Rotation = Rotation;
            this.Scale = Scale;

            this.skinningData = model.Tag as SkinningData;
            Player = new AnimationPlayer(skinningData);

            if (firstRun == false)
            {
                setNewEffect();
                firstRun = true;
            }
        }

        void setNewEffect()
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    SkinnedEffect newEffect = new SkinnedEffect(Game1.GetGraphicsDevice());
                    BasicEffect oldEffect = ((BasicEffect)part.Effect);

                    newEffect.EnableDefaultLighting();
                    newEffect.SpecularColor = Color.Black.ToVector3();

                    newEffect.AmbientLightColor = oldEffect.AmbientLightColor;
                    newEffect.DiffuseColor = oldEffect.DiffuseColor;
                    newEffect.Texture = oldEffect.Texture;

                    part.Effect = newEffect;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            TimeSpan elapsedTime = TimeSpan.FromSeconds(
                gameTime.ElapsedGameTime.TotalSeconds * PlaySpeed);

            Matrix world = Matrix.CreateScale(Scale) *
                Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) *
                Matrix.CreateTranslation(Position);

            Player.Update(elapsedTime, world);
        }

        public void Draw(Matrix View, Matrix Projection, Matrix World)
        {
            /*Matrix world = Matrix.CreateScale(Scale) *
                Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) *
                Matrix.CreateTranslation(CameraPosition);*/
            
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (SkinnedEffect effect in mesh.Effects)
                {
                    effect.World = World;
                    effect.SetBoneTransforms(Player.SkinTransforms);
                    effect.View = View;
                    effect.Projection = Projection;
                }

                mesh.Draw();
            }
        }
        
    }
}
